Component({
  data: {
    current: 0,
    isSelect: 0,
    newsList: [],
    isLoading: false,
    maxBehotTime: 0
  },

  methods: {
    onLoad(options) {
      // 这里可以添加页面加载时的逻辑
    },

    login1(e) {
      console.log(e);
      const key = e.mark.status;
      switch (key) {
        case '1':
          this.setData({
            current: 0,
            isSelect: 1
          });
          break;
        case '2':
          this.setData({
            current: 1,
            isSelect: 2
          });
          break;
        case '3':
          this.setData({
            current: 2,
            isSelect: 3
          });
          break;
        case '4':
          this.setData({
            current: 3,
            isSelect: 4
          });
          break;
        case '5':
          this.setData({
            current: 4,
            isSelect: 5
          });
          break;
        default:
          break;
      }
    },

    loadNews() {
      const that = this;
      const url = this.data.maxBehotTime === 0
        ? 'https://m.toutiao.com/list/?tag=__all__&max_time=0&min_behot_time=0&ac=wap&count=20&format=json_raw&_signature=xCd6dAAAosFlTl3HKFj1D8Qnem&i=1720748755&as=A116262940DA193&cp=66906AD149330E1&aid=1698'
        : 'https://m.toutiao.com/list/?tag=__all__&max_time=1720748755&max_behot_time=1720748755&ac=wap&count=20&format=json_raw&_signature=xCd6dAAAosFlTl3HKFj1D8Qnem&i=1720748755&as=A116262940DA193&cp=66906AD149330E1&aid=1698';

      this.setData({ isLoading: true });

      wx.request({
        url: url,
        method: 'GET',
        header: {
          'Content-Type': 'application/json'
        },
        success: (res) => {
          if (res.data && res.data.data && Array.isArray(res.data.data)) {
            let newNewsList = res.data.data.map(item => {
              // 假设 item 中有 images 和 video_cover 字段
              return {
                ...item,
                publish_days: that.calculateDays(item.publish_time),
                // 如果有视频，则使用视频封面
                video_cover: item.video_detail_info && item.video_detail_info.detail_video_large_image ? item.video_detail_info.detail_video_large_image.url : null
              };
            });
            that.setData({
              newsList: that.data.newsList.concat(newNewsList),
              // 如果响应中没有 next 字段，则不更新 maxBehotTime
              maxBehotTime: res.data.has_more ? res.data.max_behot_time : that.data.maxBehotTime,
              isLoading: false
            });
          } else {
            console.error('Invalid response data:', res.data);
            that.setData({ isLoading: false });
            wx.showToast({
              title: '加载失败',
              icon: 'none'
            });
          }
        },
        fail: () => {
          that.setData({ isLoading: false });
          wx.showToast({
            title: '加载失败',
            icon: 'none'
          });
        }
      });
    },

    calculateDays(publishTime) {
      const now = new Date().getTime();
      const publish = new Date(publishTime * 1000).getTime();
      const days = Math.floor((now - publish) / (24 * 3600 * 1000));
      return days;
    }
  }
});
